<template>
    <el-card shadow="never" class="border-0">
        <!-- 头部搜索表单 -->
        <el-form :model="searchForm" label-width="80px" size="small" class="mb-3">
            <el-row :gutter="20">
                <el-col :span="8" :offset="0">
                    <el-form-item label="搜索关键词">
                        <el-input v-model="searchForm.keyword" placeholder="管理员昵称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8" :offset="8">
                    <div class="flex items-center justify-end">
                        <el-button type="primary" @click="getData">搜索</el-button>
                        <el-button @click="resetSearchForm">重置</el-button>
                    </div>
                </el-col>
            </el-row>
        </el-form>
        <div class="flex items-center justify-between mb-4">
            <el-button type="primary" size="small" @click="handleCreate">新增</el-button>
            <el-tooltip
                effect="dark"
                content="刷新数据"
                placement="top"
            >
            <el-button text @click="getData">
                <el-icon :size="20"><Refresh /></el-icon>
            </el-button>
            </el-tooltip>
        </div>
        <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
            <el-table-column label="管理员" width="200">
                <template #default="{ row }">
                    <div class="flex items-center">
                        <el-avatar :size="40" :src="row.avatar">
                            <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
                        </el-avatar>
                        <div class="ml-3">
                            <h6>{{ row.username }}</h6>
                            <small>ID:{{ row.id }}</small>
                        </div>
                    </div> 
                </template>
            </el-table-column>
            <el-table-column label="所属角色" align="center" >
                <template #default="{ row }">
                    {{ row.role.name ? row.role.name : '-' }}
                </template>
            </el-table-column>
            <!-- 下面两个需要进行额外判断就是如果所属角色是超级管理员就不可以选中状态和进行任何操作 -->
            <el-table-column label="状态" width="120" >
                <template #default="{ row }">
                    <el-switch
                        :model-value="row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="handleStatusChange($event,row)"
                        :loading="row.statusLoading"
                        :disabled="row.super == 1"
                    />
                </template>
            </el-table-column>
            <el-table-column label="操作" width="180" align="center"> 
                <template #default="scope">
                    <small v-if="scope.row.super == 1" class="text-sm text-gray-500">暂无操作</small>
                    <div v-else>
                        <el-button type="primary" size="default" text @click="handleUpdate(scope.row)">修改</el-button>
                        <el-popconfirm 
                            title="是否要删除该管理员?"
                            confirm-button-text="确认"
                            cancel-button-text="取消"
                            @confirm="handleDelete(scope.row.id)"
                        >
                            <template #reference>
                                <el-button type="primary" size="default" text>删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <div class="flex items-center justify-center mt-5">
            <el-pagination 
                background layout="prev, pager, next" 
                :total="total" 
                :page-size="limit" 
                :current-page="currentPage"
                @current-change="getData"
            />
        </div>
        <FormDrawer ref="formDrawerRef" :title="drawTitle" destoryOnClose @submit="handleSubmit">
            <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" placeholder="密码" type="password" show-password></el-input>
                </el-form-item>
                <el-form-item label="头像">
                    <ChooseImage v-model="form.avatar"></ChooseImage>
                </el-form-item>
                <el-form-item label="所属角色" prop="role_id">
                    <el-select v-model="form.role_id" placeholder="请选中所属角色">
                        <el-option v-for="item in roles"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="0">
                    </el-switch>  
                </el-form-item>
            </el-form>
        </FormDrawer>  
    </el-card>
</template>

<script setup>
import { ref } from 'vue'
import { getManagerList,updateManagerStatus,createManager,updateManager,deleteManager } from '~/api/manager'
import FormDrawer from '~/components/FormDrawer.vue'
import ChooseImage from '~/components/ChooseImage.vue'
import { useInitTable,useInitForm } from '~/composables/useCommon.js'

const roles = ref([])

// 从封装代码导入赋值搜索表单和重置表单请求数据的功能
const {
    searchForm,
    resetSearchForm,
    tableData,
    loading,
    currentPage,
    total,
    limit,
    handleStatusChange,
    handleDelete,
    getData
} = useInitTable({
    // 搜索表单参数
    searchForm:{
        keyword:""
    },
    // 请求数据的接口
    getList:getManagerList,
    updateStatus:updateManagerStatus,
    delete:deleteManager,
    // 回调函数用于赋值数据
    onGetListSuccess:(res)=>{
        tableData.value = res.data.list.map(o => {
            o.statusLoading = false
            return o
        })
        total.value = res.data.totalCount
        roles.value = res.data.roles
    }
})

// 从封装代码导入封装的表单新增和修改(以及初始化)
const {
    drawTitle,
    formDrawerRef,
    form,
    formRef,
    rules,
    handleUpdate,
    handleCreate,
    handleSubmit
} = useInitForm({
    form:{
        username:"",
        password:"",
        role_id:'',
        status:1,
        avatar:""
    },
    getData,
    rules : {
        username:{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
        },
        password:{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
        },
        role_id:{
            required: true,
            message: '请选择角色',
            trigger: 'change'
        }
    },
    create:createManager,
    update:updateManager
})

</script>
